/**
 * 物品库存js
 */
var group_field="repartoyname";
var datarows;
var grid='';
var exportString='';
var filename="库存使用记录";
var curdate=null;
/**打印时候用的url**/
var pageurl="";
$(function(){
	grid=$("#goods").datagrid({
		 singleSelect:true,
         collapsible:false,
         pagination: true, 
         rownumbers:true,
         fitColumns:true,
         fit:true,
         remoteSort:false,
         pageList: [100,200,500,1000,10000,1000000000],
         pageSize: 100,
 	     showFooter:true,
         url:path+'report/reportalldate.html', 
         view:groupview,
         groupField:group_field,
         groupFormatter:function(value,rows){
     			var sumNum = 0;
     			var css="#DD4E42";
     			for(var i=0;i<rows.length;i++){
     				sumNum += Number(rows[i].goodnumber);
     			}	
     		 if(sumNum>0){
     			css="#0483D2";
     		 }else{
     			css="#DD4E42";
     		 }
             return value + ' - ' + rows.length + '项数据'+' - 数量总计：<span style="font-size:25px;color:'+css+'">'+sumNum.toFixed(2)+"</span>";
         },
         onLoadSuccess : function(data) {
        	 var alltotal=data.alltotal;
        	 if(alltotal==null){
        		 alltotal=0.0
        	 }
              // $('#goods').datagrid('statistics',alltotal);
 			},
 		loadFilter: function(data){
 			datarows=data;
				return data;				
			},
		onHeaderContextMenu:function(e, field){ //右键
			e.preventDefault();
	    	$("#div_rightmenu").menu('show',{    
				  left: e.pageX,    
				  top: e.pageY    
			});
		},
		onRowContextMenu:function(e, rowIndex, rowData){
			e.preventDefault();
	    	$("#div_rightmenu").menu('show',{    
				  left: e.pageX,    
				  top: e.pageY    
			});
		},
	    columns:[[ 
	        {field:'billno',title: '库存编号',align: 'center',width:75,sortable:true},  
			{field:'clientname',title: '客户名称',align: 'center',width:65,sortable:true},  
			{field:'goodname',title: '物品名称',align: 'center',width:65,sortable:true},
			{field:'specifications',title: '规格型号',align: 'center',width:65,sortable:true},
			{field:'goodnumber',title: '物品数量',align: 'center',width:65,sortable:true},
			{field:'unit',title: '单位',align: 'center',width:65,sortable:true},
			{field:'snno',title: 'SN',align: 'center',width:65,sortable:true},
			{field:'repartoyname',title: '仓库',align: 'center',width:65,sortable:true},
			{field:'repartoyrack',title: '货架',align: 'center',width:65,sortable:true},
			{field:'repartoyaddress',title: '货架位置',align: 'center',width:65,sortable:true},
			{field:'positionnumber',title: '位置编号',align: 'center',width:65,sortable:true},
			{field:'couriernumber',title: '快递单号',align: 'center',width:65,sortable:true},
			//{field:'goodsbillno',title: '物品编码',align: 'center',width:65,sortable:true},
			{field:'goodsgoout',title: '物品去向',align: 'center',width:65,sortable:true},
			{field:'leadingperson',title: '操作人',align: 'center',width:65,sortable:true},
			{field:'leadingtime',title: '操作时间',align: 'center',width:65,sortable:true	},
			{field:'remark',title: '备注',align: 'center',width:65,sortable:true	},
			{field:'billtype',title: '单据类型',align: 'center',width:65,sortable:true}
	    ]]   

	}); 

	
	//分页显示
	  var p = $('#goods').datagrid('getPager'); 
	    $(p).pagination({ 
	        pageSize: 100,//每页显示的记录条数，默认为10 
	        pageList: [100,200,500,1000,10000,1000000000],//可以设置每页记录条数的列表 
	        beforePageText: '第',//页数文本框前显示的汉字 
	        afterPageText: '页    共 {pages} 页', 
	        displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录'
	    });   
   
		});
/**
 *自由设置分组统计模式 
 * @param group_field
 */
function groupfieldAuto(group_field){
	grid=$("#goods").datagrid({
		 singleSelect:true,
        collapsible:true,
        pagination: true, 
        rownumbers:true,
        fitColumns:true,
        remoteSort:false,
        pageList: [100,200,500,1000,10000,1000000000],
        pageSize: 100,
	     showFooter:true,
        url:path+'report/reportalldate.html', 
        view:groupview,
        groupField:group_field,
        groupFormatter:function(value,rows){
    			var sumNum = 0;
    			for(var i=0;i<rows.length;i++){
    				sumNum += Number(rows[i].goodnumber);
    			}	
        		 if(sumNum>0){
          			css="#0483D2";
          		 }else{
          			css="#DD4E42";
          		 }
                  return value + ' - ' + rows.length + '项数据'+' - 数量总计：<span style="font-size:25px;color:'+css+'">'+sumNum.toFixed(2)+"</span>";
           // return value + ' - ' + rows.length + '项数据'+' - 总计:'+sumNum.toFixed(2);
        },
        onLoadSuccess : function(data) {
       	 var alltotal=data.alltotal;
       	 if(alltotal==null){
       		 alltotal=0.0
       	 }
             // $('#goods').datagrid('statistics',alltotal);
			},
		loadFilter: function(data){
			datarows=data;
				return data;
			},
		onHeaderContextMenu:function(e, field){ //右键
			e.preventDefault();
	    	$("#div_rightmenu").menu('show',{    
				  left: e.pageX,    
				  top: e.pageY    
			});
		},
	    columns:[[ 
	        {field:'billno',title: '库存编号',align: 'center',width:75,sortable:true},  
			{field:'clientname',title: '客户名称',align: 'center',width:65,sortable:true},  
			{field:'goodname',title: '物品名称',align: 'center',width:65,sortable:true},
			{field:'specifications',title: '规格型号',align: 'center',width:65,sortable:true},
			{field:'goodnumber',title: '物品数量',
				//avg:true, 
				//min:true,
				//max:true,
				//sumall:true,
				//sum:true,
				align: 'center',width:65,sortable:true},
				{field:'unit',title: '单位',align: 'center',width:65,sortable:true},
			{field:'snno',title: 'snno',align: 'center',width:65,sortable:true},
			{field:'repartoyname',title: '仓库',align: 'center',width:65,sortable:true},
			{field:'repartoyrack',title: '货架',align: 'center',width:65,sortable:true},
			{field:'repartoyaddress',title: '货架位置',align: 'center',width:65,sortable:true},
			{field:'positionnumber',title: '位置编号',align: 'center',width:65,sortable:true},
			{field:'couriernumber',title: '快递单号',align: 'center',width:65,sortable:true},
			
			//{field:'goodsbillno',title: '物品编码',align: 'center',width:65,sortable:true},
			{field:'goodsgoout',title: '物品去向',align: 'center',width:65,sortable:true},
			{field:'leadingperson',title: '操作人',align: 'center',width:65,sortable:true},
			{field:'leadingtime',title: '操作时间',align: 'center',width:65,sortable:true
			},
			
			{field:'billtype',title: '单据类型',align: 'center',width:65,sortable:true}
	    ]]   

	}); 
	//分页显示
	  var p = $('#goods').datagrid('getPager'); 
	    $(p).pagination({ 
	        pageSize: 100,//每页显示的记录条数，默认为10 
	        pageList: [100,200,500,1000,10000,1000000000],//可以设置每页记录条数的列表 
	        beforePageText: '第',//页数文本框前显示的汉字 
	        afterPageText: '页    共 {pages} 页', 
	        displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录'
	    });   
}
function jsonDateFormat(jsonDate) {
	   //json日期格式转换为正常格式
	    try {
	        var date = new Date(parseInt(jsonDate.replace("/Date(", "").replace(")/", ""), 10));
	        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
	        var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
	        var hours = date.getHours();
	        var minutes = date.getMinutes();
	        var seconds = date.getSeconds();
	        var milliseconds = date.getMilliseconds();
	        return date.getFullYear() + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds + "." + milliseconds;
	    } catch (ex) {
	        return "";
	    }
	}

/***
 * 选择字段查询数据
 * @param value
 * @param name
 */
function searcherGoods(value,name){
	var url=path+'report/reportalldate.html?field='+name+"&searchertext="+value;
	url=encodeURI(url);
	url=encodeURI(url);
	pageurl=url;
	$("#goods").datagrid("load",url);
}
/**
 * 右键点击后的事件
 */
function menuHandler(item){
	if(item.name=='expcurpage'){
	//	filename="库存报表";
		expt(grid);  
	}else if(item.name=='expallpage'){
		
	}else if(item.name=='printcurpage'){
		//pageurl=path+'report/reportalldate.html?1=1';
		//PrintOneURL(pageurl);
		//CreateFormPage("打印本页明细", $("#goods"));
		print(grid);  
	}else if(item.name=='printallpage'){
		
	}else{ //分组
		group_field=item.name;
		//searcherGoods(null,null);
		groupfieldAuto(group_field);
	}

}



/**
 * 打印
 */
var LODOP; //声明为全局变量      
function PrintOneURL(strurl){
	LODOP=getLodop();  
	LODOP.PRINT_INIT("打印控件功能演示_Lodop功能_按网址打印");
	//LODOP.ADD_PRINT_URL(30,20,746,"95%",document.getElementById(strID).value);
	LODOP.ADD_PRINT_URL(30,20,746,"95%",window.location.href);
	LODOP.SET_PRINT_STYLEA(0,"HOrient",3);
	LODOP.SET_PRINT_STYLEA(0,"VOrient",3);
//	LODOP.SET_SHOW_MODE("MESSAGE_GETING_URL",""); //该语句隐藏进度条或修改提示信息
//	LODOP.SET_SHOW_MODE("MESSAGE_PARSING_URL","");//该语句隐藏进度条或修改提示信息
	LODOP.PREVIEW();			
};